package rouler.bot;

import java.util.Random;

import rouler.action.partycasino.european.RouletteAction;
import rouler.roulette.Colors;

public class Rouler {

	RouletteAction ra;
	
	float betSize = 0.1f;
	
	float bet = betSize;
	
	float stack = 10f;
	
	Random rnd;
	Random[] rnds = new Random[999];
	
	public Rouler(RouletteAction ra){
		this.ra = ra;
		for(int i=0;i<rnds.length;i++){
			rnds[i] = new Random((long)(Math.random()*System.currentTimeMillis()+10000));
		}
		this.rnd = new Random(System.currentTimeMillis());
	}
	
	public void play() throws InterruptedException{
		Colors betColor = rnd.nextInt(2)==0?Colors.black:Colors.red;
		
		while(true){
			// setzen
			stack -= bet;
			if(stack <= 0) {
				System.out.println("STOP LOW STACK");
				break;
			}else if(stack >= 14f){
				System.out.println("STOP HIGH STACK");
				break;
			}
			ra.bet(betColor);
			double betting = betSize;
			while(betting<bet){
				ra.doubleBet();
				betting = betting*2;
			}
			Thread.sleep(1000);
			// drehen
			Thread.sleep(1060);
			ra.spin();
			// warten bis gedreht
			do{
				System.out.println("wait for spin");
				Thread.sleep(1400);				
			}while(ra.isSpinning());
			// checken ob gewonnen und einsatz + farbe entspr. anpassen
			Colors colorResult = ra.getColor();
			boolean won = colorResult.equals(betColor) || colorResult.equals(Colors.green);
			betColor = rnds[rnd.nextInt(rnds.length)].nextInt(2)==0?Colors.black:Colors.red;
			if(won){
				stack += colorResult.equals(Colors.green)?(bet / 2f):(bet * 2f);
				bet = colorResult.equals(Colors.green)?bet:betSize;
				System.out.println("Won - betting " + betColor);
			}else{
				bet = bet * 2f;
				System.out.println("Lost - double betting " + betColor);
			}
			System.out.println("Stack: " + stack);
			Thread.sleep(1200);
		}
		
	}
	
}
